/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.manage;

import org.zhiqim.account.dbo.sys.ZacPayment;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 *  支付方式管理
 * @version v1.0.0 @author zzy 2018-4-9 新建与整理
 */
public class PaymentAction extends StdSwitchAction
{

    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("paymentCode", "请选择一个选项"));
    }

    @Override
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsLen("paymentCode", "支付方式编码不能为空且不超过32个字符", 1, 32));
        request.addValidate(new IsLen("paymentName", "支付方式名称不能为空且不超过32个字符", 1, 32));
        request.addValidate(new IsLen("paymentIco", "支付方式图标不能为空且不超过32个字符", 1, 32));
        request.addValidate(new IsIntegerValue("paymentSeq", "支付方式排序必须是[0, 999999]范围的非负整数", 0, 999999));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        int page = request.getParameterInt("page", 1);
        int pageSize = request.getContextAttributeInt("zmr_page_size", 20);
        
        PageResult<ZacPayment> result = Global.get(ZTable.class).page(ZacPayment.class, page, pageSize, new Selector().addOrderbyAsc("paymentSeq"));
        result.addConditionMap(request.getParameterMap());
        
        request.setAttribute("result", result);
    }

    @Override
    protected void add(HttpRequest request) throws Exception
    {
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        String paymentCode = request.getParameter("paymentCode");
        ZacPayment item = Global.get(ZTable.class).item(ZacPayment.class, paymentCode);
        if (item == null)
        {
            request.returnHistory("该支付方式不存在");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        String paymentCode = request.getParameter("paymentCode");
        if (Global.get(ZTable.class).count(ZacPayment.class, paymentCode) == 1)
        {
            request.returnHistory("该支付方式已添加");
            return;
        }
        
        ZacPayment payment = new ZacPayment();
        payment.setPaymentCode(paymentCode);
        payment.setPaymentName(request.getParameter("paymentName"));
        payment.setPaymentIco(request.getParameter("paymentIco"));
        payment.setPaymentStatus(0);
        payment.setPaymentSeq(request.getParameterInt("paymentSeq"));
        
        Global.get(ZTable.class).insert(payment);
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        Updater updater = new Updater();
        updater.addMust("paymentCode", request.getParameter("paymentCode"));
        updater.addField("paymentName", request.getParameter("paymentName"));
        updater.addField("paymentIco", request.getParameter("paymentIco"));
        updater.addField("paymentStatus", request.getParameterInt("paymentStatus"));
        updater.addField("paymentSeq", request.getParameterInt("paymentSeq"));
        
        Global.get(ZTable.class).update(ZacPayment.class, updater);
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        String paymentCode = request.getParameter("paymentCode");
        if (Global.get(ZTable.class).count(ZacPayment.class, paymentCode) == 0)
        {
            request.returnHistory("该支付方式不存在");
            return;
        }
        
        Global.get(ZTable.class).delete(ZacPayment.class, paymentCode);
    }

}
